<template>
	<view class="loginBox">
		<view class="img"></view>
		<view class="title" @click="back">欢迎注册禹冰之家</view>
		<view class="content">
			<view class="inputbox">
				<view class="inputboxtext">用户名</view>
				<input v-model="form.userName" placeholder-style="color:#ccc;font-size:30rpx" type="text" placeholder="请输入用户名" />
			</view>
			<view class="inputbox">
				<view class="inputboxtext">密码</view>
				<input v-model="form.password" placeholder-style="color:#ccc;font-size:30rpx" type="password" placeholder="请输入密码" />
			</view>
			<view class="inputbox" style="justify-content: space-between;">
				<view style="display: flex;align-items: center;">
					<view class="inputboxtext">手机号</view>
					<input v-model="form.phone" style="width: 230rpx;" placeholder-style="color:#ccc;font-size:30rpx" type="number" placeholder="请输入手机号" />
				</view>
				
			  <view v-if="!count_down" @click="getMsgCode()"
			  	style="margin-right: 20rpx; text-align: center;line-height: 64rpx;font-size: 24rpx;color: #fff; width: 150rpx;height: 64rpx;background: #3381FF;border-radius:10rpx;">
			  	获取验证码</view>
			  <view v-else
			  	style="margin-right: 20rpx;text-align: center;line-height: 64rpx;font-size: 24rpx;color: #fff; width: 150rpx;height: 64rpx;background: #ccc;border-radius:10rpx;">
			  	重新获取{{ count_down }}</view>
			</view>
			<view class="inputbox">
				<view class="inputboxtext">验证码</view>
				<input v-model="form.code" placeholder-style="color:#ccc;font-size:30rpx" type="password" placeholder="请输入验证码" />
			</view>
			<view class="login" @click="registerfun">注册</view>
			<view class="tips" @click="toPhone">已有账号？<span class="register">点击登录</span></view>
		</view>
		
	</view>
</template>

<script setup>
  import{ref,reactive,getCurrentInstance} from 'vue'
	const {proxy} = getCurrentInstance()
	const form = reactive({
		userName:'',
		password:'',
		phone:'',
		code:''
	})
	const count_down = ref('')
	// 点击获取验证码时开始倒计时
	const getMsgCode=()=> {
		form.code = ''
		let reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
		if (!reg.test(form.phone) || form.phone == "") {
			form.phone= ''
			uni.showToast({
				title: '请输入正确的手机号码！',
				icon: 'none',
				duration: 700
			})
			return
		}
		// 请求接口获取验证码，然后倒计时
		count_down.value = 60 // 倒数60秒
		countDown()
		hqyam() //开始发送验证码
	}
	
	// 倒计时
	const countDown=()=> {
		const timer = setInterval(() => {
			count_down.value = count_down.value - 1
			if (count_down.value < 10) count_down.value = '0' + count_down.value
			if (count_down.value <= 0) {
				clearInterval(timer)
				count_down.value = ''
			}
		}, 1000)
	}
	const toPhone=()=> {
		uni.navigateBack()
	}
	// 获取验证码
	const hqyam = async()=> {
		console.log('获取验证码');
		var data = {
			userName: form.userName ,
			phone: form.phone 
		}
		const result = await proxy.$api.rigisteryzmAPi(data)
		if(result.code==200){
			console.log('获取验证码',result);
		}
	}
	
	const registerfun = async()=>{
		var data = form
		const result = await proxy.$api.rigisterAPi(data)
		if(result.code==200){
			console.log('注册',result);
			proxy.$showToast('注册成功')
			login()
		}
	}
	const logindata = reactive({
		username:form.userName,
		password:form.password,
		code:'123'
	})
	const login = async()=>{
		const result = await proxy.$api.loginAPi(logindata)
		if(result.code == 200){
			console.log('登录',result);
			access_tokenFun(result.data.access_token)
			uni.setStorageSync('istype','师傅端')
			uni.reLaunch({
			  url:'/pages/home/home?istype='+'师傅端'
			})
		}
	}
			
	
</script>

<style lang="scss">
	.loginBox {
		height: 100vh;
		background-color: white;
		position: relative;
		z-index: +1;
		background-color: #ccc;
		.img {
			z-index: -1;
			position: absolute;
			top: 0;
			height: 100vh;
			width:750rpx;
			background: linear-gradient(to bottom, #4E8CFF, #80c4ff, rgba(0, 0, 0, 0));
		}
		.title {
			display: flex;
			align-items: center;
			justify-content: center;
			font-family: '阿里妈妈数黑体';
			color: white;
			font-size: 60rpx;
			padding-top: 300rpx;
			box-sizing: border-box;
		}

		.content {
			background-color: white;
			margin: 0 auto;
			margin-top: 80rpx;
			border-radius: 20rpx;
			width: 690rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			padding-bottom: 60rpx;box-sizing: border-box;
			.inputbox{
				width: 600rpx;height: 90rpx;border-radius: 20rpx;
				border: 1rpx solid #ccc;
				margin-top: 60rpx;display: flex;align-items: center;
				.inputboxtext{
					width: 130rpx;color: #333;font-size: 32rpx;margin-left: 30rpx;
				}
				input{
					padding-left: 30rpx;box-sizing: border-box;
					width: 300rpx;color:#333
				}
			}

			.logo {
				width: 180rpx;
				margin: 40rpx 0;
			}
			.agreement {
				display: flex;
				margin-top: 30rpx;
				align-items: center;
				justify-content: center;
				margin-bottom: 40rpx;
				width: 600rpx;
			}

			.login {
				background-color: #3381FF;
				color: white;
				width: 600rpx;
				text-align: center;
				padding: 20rpx 0;
				border-radius: 10rpx;
				margin: 0 auto;
				margin-top: 60rpx ;
			}
			.tips {
				color: #ccc;
				font-size: 28rpx;
				margin-top: 30rpx;

				.register {
					color: #4E8CFF;
				}
			}
		}

		
	}
</style>